
<template>
  <el-dialog v-model="ifShow" :title="modelTitle" width="1000" :before-close="handleCancel">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="auto" class="demo-ruleForm" status-icon>
      <el-form-item label="供应商" prop="supplierId">
        <el-select filterable clearable v-model="formData.supplierId" placeholder="请选择">
          <el-option v-for="item in supplierOptions" :key="item.id" :label="item.companyName" :value="item.id">{{ item.companyNo }}-{{ item.companyName }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="渠道" prop="channelId">
        <el-select multiple clearable v-model="formData.channelId" placeholder="请选择">
          <el-option v-for="item in channelArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型" prop="bizType">
        <el-select multiple clearable v-model="formData.bizType" placeholder="请选择">
          <el-option v-for="item in bizTypeArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="平台" prop="platformId">
        <el-select multiple clearable v-model="formData.platformId" placeholder="请选择">
          <el-option v-for="item in platFormArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="城市" prop="cityId">
        <el-select filterable clearable v-model="formData.cityId" placeholder="请选择" @change="handleCityChange">
          <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="车型" prop="vehicleTypeId">
        <el-select multiple clearable v-model="formData.vehicleTypeId" placeholder="请选择">
          <el-option v-for="item in carTypeArr" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="派单比例" prop="percent">
        <el-input v-model="formData.percent" placeholder="请输入派单比例 单位：%"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleOk">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import CommonApi from "@/api/common";
import Supplier from "@/api/supplier";
import { channelArr, bizTypeArr, orderTypeArr, platFormArr, carTypeArr } from "@/enums/common";

const props = defineProps<{}>();
const emit = defineEmits(["success"]);
const formRef = ref(null);
const formData = reactive({
  id: "",
  supplierId: undefined,
  bizType: undefined,
  platformId: undefined,
  cityId: undefined,
  cityName: "",
  vehicleTypeId: undefined,
  percent: undefined,
  channelId: undefined,
});
const rules = reactive({
  supplierId: [{ required: true, message: "请选择供应商" }],
  bizType: [{ required: true, message: "请选择业务类型" }],
  platformId: [{ required: true, message: "请选择平台" }],
  cityId: [{ required: true, message: "请选择城市" }],
  channelId: [{ required: true, message: "请选择渠道" }],
  vehicleTypeId: [{ required: true, message: "请选择车型" }],
  percent: [{ required: true, message: "请输入派单比例 单位：%!" }],
});
const ifShow = ref(false);
const modelTitle = ref();
const supplierOptions = ref();
const cityOptions = ref();
function edit(title: any, row: any) {
  nextTick(() => {
    modelTitle.value = title;
    ifShow.value = true;
    Object.assign(formData, row);
    formData.channelId = [row.channelId];
    formData.bizType = [row.bizType];
    formData.platformId = [row.platformId];
    formData.vehicleTypeId = [row.vehicleTypeId];
  });
}

//城市下拉列表
function handleCityChange(value) {
  console.log("value", value);

  formData.cityName = cityOptions.value.find((item) => item.id == value).name;
}
/**
 * 确定按钮点击事件
 */
//转换判断当前数据是否数组类型
// const ensureArray = (value) => (typeof value !== "object" ? [value] : value);
async function handleOk() {
  await formRef.value.validate((valid, fields) => {
    if (valid) {
      // formData.bizType = ensureArray(formData.bizType);
      // formData.platformId = ensureArray(formData.platformId);
      // formData.vehicleTypeId = ensureArray(formData.vehicleTypeId);
      // formData.channelId = ensureArray(formData.channelId);

      Supplier.editSupplierConfig(formData).then((res) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
          formRef.value.resetFields();
          emit("success");
          handleCancel();
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  formRef.value.resetFields();
  ifShow.value = false;
}

defineExpose({
  edit,
});
onMounted(() => {
  CommonApi.getSupplier({ pageNo: 1, pageSize: 9999, status: 1 }).then((data) => {
    if (data.code == 0) {
      supplierOptions.value = data.result;
    }
  });
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.container {
  font-size: 16px;
  padding: 14px;
  .info {
    margin-top: 22px;
    .aud {
      margin-top: 14px;
      width: 38%;
    }
  }
}
</style>
